import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_getx/ext/ext.dart';
import 'package:flutter_getx/res/dimens/dimens.dart';
import 'package:get/get.dart';

import 'load_image.dart';


class StateLayout extends StatelessWidget {
  
  const StateLayout({
    Key? key,
    required this.type,
    this.hintText,
    this.onTap,
  }):super(key: key);
  
  final StateType type;
  final String? hintText;
  final GestureTapCallback? onTap;
  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      onTap: type == StateType.loading ? null : onTap,
      child: Column(
        crossAxisAlignment: CrossAxisAlignment.center,
        mainAxisAlignment: MainAxisAlignment.center,
        children: <Widget>[
          SizedBox(height: Dp.dp_50,),
          if (type == StateType.loading)
            const CupertinoActivityIndicator(radius: 16.0)
          else
            Opacity(
              opacity: Get.isDarkMode ? 0.5 : 1,
              child: LoadAssetImage(
                type.info.img.pathState,
                width: type.info.imgWidth,
              ),
            ),
          SizedBox(width: double.infinity, height: Dp.dp_10,),
          Text(
            hintText ?? type.info.hint,
            style: Theme.of(context).textTheme.subtitle2?.copyWith(fontSize: Sp.sp14),
          ),
          SizedBox(height: Dp.dp_50,),
        ],
      ),
    );
  }
}

enum StateType {
  /// 加载中
  loading,
  /// 空
  empty,
  /// 无网络
  noNetwork,
  /// 出错
  error,
}

extension StateTypeExtension on StateType {

  StateInfo get info => <StateInfo>[
    const StateInfo('', ''),//loading
    const StateInfo('empty_records', 'No data'),//empty
    const StateInfo('empty_records', 'Network error'),//no_network
    const StateInfo('empty_records', 'Service error'),//error
  ][index];

}


class StateInfo{
  final String img;
  final String hint;
  final double imgWidth;

  const StateInfo(this.img, this.hint, {this.imgWidth = 120});
}